//
//  ALLNavigationBar.m
//  CarHome
//
//  Created by lanou3g on 15/4/29.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "ALLNavigationBar.h"

@implementation ALLNavigationBar

#pragma mark - 导航栏的初始化方法
- (instancetype)initWithFrame:(CGRect)frame
{
    
    self = [super initWithFrame:frame];
    if (self != nil) {
        
        self.backgroundColor = [UIColor whiteColor];
        [self addAllViews];
    }
    
    
    return self;
    
}

#pragma mark - 给导航栏添加控件（添加集合视图，和labelItem）
- (void)addAllViews
{
    
    UICollectionViewFlowLayout *layer = [[UICollectionViewFlowLayout alloc] init];//创建流水布局
    layer.itemSize = CGSizeMake(70, kNBHEIGHT);//设置每个格子的尺寸
    layer.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    //layer.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);//设置整个collectionView的内边距
    layer.minimumLineSpacing = 0.0;//设置每一行之间的间距
    self.collectionView = [[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kNBWIDTH, kNBHEIGHT) collectionViewLayout:layer]autorelease];////初始化 UICollectionViewController
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.bounces = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    [self addSubview:_collectionView];
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    self.labelItem = [[[UILabel alloc] initWithFrame:CGRectMake(10, kNBHEIGHT - 3,70 - 20, 3)] autorelease];
    _labelItem.backgroundColor = [UIColor blueColor];
    [_collectionView addSubview:_labelItem];
    
    
}


#pragma mark - 设置有几个分组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

#pragma mark - 设置每个分组有多少item
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 7;
}

#pragma mark - 设置每个item的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"indentifier";
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    
    NSArray *a = @[@"最新", @"新闻", @"评测", @"导购", @"行情", @"改装", @"游记"];
    
    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 40, kNBWIDTH, 20)]autorelease];
    label.text = a[indexPath.row];
    label.tag = indexPath.row + 1;
    label.textAlignment = NSTextAlignmentCenter;
    cell.backgroundView = label;

    cell.backgroundColor = [UIColor whiteColor];
    
    return cell;
    
}



#pragma mark - 点击item触发方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
   
    [self slideLabelWithIndexPath:indexPath];//点击item使labelItem移动
    
    [self relatedTableViewWithIndexPath:indexPath WithCollectionView:collectionView];//点击item使tableView随之移动
    
    [self promoteUserExperienceWithIndexPath:indexPath];
        
}

#pragma mark - 点击item使tableView随之移动
-(void)relatedTableViewWithIndexPath:(NSIndexPath *)indexPath WithCollectionView:(UICollectionView *)collectionView
{
    
    CGFloat x = indexPath.row * kNBWIDTH;
    _scrollView.contentOffset = CGPointMake(x, 0);
    
}


#pragma mark - 点击item使labelItem移动
- (void)slideLabelWithIndexPath:(NSIndexPath *)indexPath
{
    
    CGRect f = _labelItem.frame;
    f.origin.x = indexPath.row * 70 + 10;
    
    [UIView animateWithDuration:0.4 animations:^{
        
        _labelItem.frame = f;
        
    }];
    
}


- (void)promoteUserExperienceWithIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [_collectionView cellForItemAtIndexPath:indexPath];
    UILabel *label = (UILabel *)cell.backgroundView;
    if ([label.text isEqualToString:@"行情"]) {
        
        _collectionView.contentOffset = CGPointMake(110, 0);
        
    }
    if ([label.text isEqualToString:@"导购"]) {
        
        _collectionView.contentOffset = CGPointMake(0, 0);
    }

}



@end
